﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Rodunu.Business.Interfaces;
using Rodunu.Business.Models.User;

namespace Rodunu.Clients.WPF.Services.Amazon
{
	/// <summary>
	/// Interaction logic for DetailPage.xaml
	/// </summary>
	public partial class DetailPage
    {
        private readonly IManageSettings _manageSettings;
        private readonly Account _account;

        public DetailPage(IManageSettings manageSettings)
		{
			InitializeComponent();

            _manageSettings = manageSettings;

            CloseButton.Click += CloseButton_Click;

            if (_manageSettings.CurrentUser != null)
            {
                _account = (from act in _manageSettings.CurrentUser.Accounts
                            where act.AccountType == AccountTypes.Amazon
                            select act).SingleOrDefault();
                if (_account == null)
                {
                    _account = new Account();
                    _account.AccountType = AccountTypes.Amazon;
                    _manageSettings.CurrentUser.Accounts.Add(_account);
                }
            }

            IsEnabledCheckBox.IsChecked = _account.IsEnabled;
		}

        void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            _account.AccountType = AccountTypes.Amazon;
            _account.IsEnabled = (bool)IsEnabledCheckBox.IsChecked;

            _manageSettings.SaveCurrentSettings();
        }
	}
}